package com.tmall.testgen.client.socket;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.common.bo.interact.apply.ApplyConnection;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo4Transfer;
import com.tmall.testgen.common.bo.interact.back.LoadedClassAndMethod;
import com.tmall.testgen.common.bo.interact.back.TransferCarFromClient;
import com.tmall.testgen.common.bo.interact.back.TransferCarFromServer;
import com.tmall.testgen.common.bo.interact.back.syn.BaseInfo;
import com.tmall.testgen.common.bo.interact.back.syn.ConnectionSuccResult;
import com.tmall.testgen.common.socket.ReceiveObjectHandler;
import com.tmall.testgen.common.socket.syn.SynCallBackCenter;
import com.tmall.testgen.common.socket.syn.SynObject;
import com.tmall.testgen.common.util.InterceptorConvertor;
import com.tmall.testgen.plugin.ConsoleFactory;

/**
 * @author haojia
 * @date 2014-07-04
 */
public class ClientReceiveObjectHandler implements  ReceiveObjectHandler  {


	@Override
	public void handleReceiveObj(Object receive) {
		
		if(!(receive instanceof TransferCarFromServer)){
			return;
		}
		
		TransferCarFromServer car=(TransferCarFromServer) receive;
			
		 if( car.getData() instanceof ConnectionSuccResult){
             SocketClient.getInstance().setConnectionSuccResult((ConnectionSuccResult) car.getData());
            return ;
        }
		if(car.getData() instanceof SynObject){
			SynCallBackCenter.getInstance().handleReceive((SynObject) car.getData());
			return;
		}
		
		  
		switch(car.getType()){
			case TransferCarFromServer.TYPE_USER_ID:
				handleReceiveUserId((String) car.getData());
				break;
			case TransferCarFromServer.TYPE_INTERCEPT_INFO:
				handleReceiveInterceptInfo((InterceptInfo4Transfer) car.getData());
				break;
			case TransferCarFromServer.TYPE_LOADED_CLASS_AND_METHOD:
				handleReceiveLoadedClassAndMethod((LoadedClassAndMethod) car.getData());
				break;
		
		}
			
	}
	
	
	/**
	 * 收到userId的处理
	 * @param userId
	 */
	private void handleReceiveUserId(String userId){
		ClientDataCenter.getInstance().setUserId(userId);
		//接收到消息后马上发送规则
		ApplyConnection applyConnection=new ApplyConnection();
		applyConnection.setInterceptDes(ClientDataCenter.getInstance().getInterceptDes());
		applyConnection.setIp(ClientDataCenter.getInstance().getIp());
		applyConnection.setMockDes(ClientDataCenter.getInstance().getMockDes());
		applyConnection.setPid(ClientDataCenter.getInstance().getPid());
		applyConnection.setUserId(userId);
		
		
		SocketClient.getInstance().writeObect(new TransferCarFromClient(TransferCarFromClient.TYPE_APPLY_CONNECTION, applyConnection));
	}
	
	/**
	 * 收到拦截信息的处理
	 * @param info
	 * @throws Exception 
	 */
	private void handleReceiveInterceptInfo(InterceptInfo4Transfer info){
		try{
			ClientDataCenter.getInstance().addUserInfo(InterceptorConvertor.transferInterceptInfo2InterceptInfo(info));
		}catch(Exception e){
			ConsoleFactory.putError(e);
		}
	}
	
	/**
	 * 收到已经加载的类和方法信息处理
	 */
	private void handleReceiveLoadedClassAndMethod(LoadedClassAndMethod  la){
		System.out.println("接收LoadedClassAndMethod"+la.getMethod());
		if(la.getType()==BaseInfo.TYPE_INTERCEPT){
			ClientDataCenter.getInstance().addLoadedInterceptClassAndMethods(la.getClassName(), la.getMethod());
		}else{
			ClientDataCenter.getInstance().addLoadedMockClassAndMethods(la.getClassName(), la.getMethod());
		}
	}
}
